var goods;
var members;

layui.use(['form'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.$;
    
    var dealerSelect = document.getElementById("dealer_id");
    $.ajax({
        url:'/Dealer/list',
        type: "get",
        data: "?page=1&limit=15",
        datatype:"json",
        success:function(data){
            let optEle1 = document.createElement("option");
            optEle1.setAttribute("value","default");
            dealerSelect.appendChild(optEle1);
            for (var x=0 ; x<data.data.length;x++){
                let optEle1 = document.createElement("option");
                optEle1.setAttribute("value",data.data[x].id);
                optEle1.appendChild(document.createTextNode(data.data[x].dealer));
                dealerSelect.appendChild(optEle1);
            }
            layui.form.render("select");
        }
    });

    var goodSelect = document.getElementById("goods_id");
    $.ajax({
        url:'/Goods/list',
        type: "post",
        data: JSON.stringify({ "page": 1, "limit": 15, "searchParams": {}}),
        datatype:"json",
        success: function (res) {
            // console.log("Debug resdata:",res.data[1].uid);
            let optEle1 = document.createElement("option");
            optEle1.setAttribute("value","default");
            goodSelect.appendChild(optEle1);
            for (var x=0 ; x<res.data.length;x++){
                let optEle1 = document.createElement("option");
                optEle1.setAttribute("value",res.data[x].goods_id);
                optEle1.appendChild(document.createTextNode(res.data[x].goods));
                goodSelect.appendChild(optEle1);
            }
            layui.form.render("select");
        }
    });

    // var memSelect = document.getElementById("member");
    // $.ajax({
    //     url:'/Member/list',
    //     type: "post",
    //     data: JSON.stringify({ "page": 1, "limit": 15, "searchParams": {}}),
    //     datatype:"json",
    //     success: function (res) {
    //         console.log("Debug resdata:",res.data);
    //         let optEle1 = document.createElement("option");
    //         optEle1.setAttribute("value","default");
    //         memSelect.appendChild(optEle1);
    //         for (var x=0 ; x<res.data.length;x++){
    //             let optEle1 = document.createElement("option");
    //             optEle1.setAttribute("value",res.data[x].mem_uid);
    //             optEle1.appendChild(document.createTextNode(res.data[x].username));
    //             memSelect.appendChild(optEle1);
    //         }
    //         layui.form.render("select");
    //     }
    // });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        var confirm = layer.confirm("确认提交？", {
            // title: '确认提交？'
        }, function () {
            console.log("Debug querydata:",JSON.stringify([data.field]));
            $.ajax({
                url:"/Order/add",
                type:"post",
                data:JSON.stringify([data.field]),
                datatype:"json",
                success: (res) => {
                    layer.close(confirm);
                    // console.log("Debug add uuid:",data.field);
                    var alert = layer.alert(JSON.stringify(res), {
                        title: '最终的提交信息'
                    }, function () {
                        // 关闭弹出层
                        layer.close(alert);

                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                }
            });
        });

        return false;
    });
});